package cn.iocoder.yudao.module.lifefly.web.controller.admin;

import cn.iocoder.yudao.framework.security.core.util.SaBaseController;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController extends SaBaseController {


    @Resource
    private FlowExecutor flowExecutor;
    @Value("${liteflow.rule-source}")
    private String flowFile;


    @PostMapping(value = "/testJS", produces = "application/json")
    public Object testJS(String js) {
        log.info("flowfile is {}", flowFile);
        flowExecutor.reloadRule();
        Map<String, Object> result = new HashMap<>();
        LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg", result);
        Map contextBean = response.getContextBean(result.getClass());
        log.info("result:{}", contextBean.get("result"));
        return "ERROR";
    }
}
